<?php


namespace app\api\controller\xilumarket;


use app\common\controller\Api;
use app\common\model\xilumarket\Area;
use OSS\OssClient;

class Common extends Api
{
    protected $noNeedLogin = ['init_config','params','area_tree','get_city_by_lat','cities'];
    protected $noNeedRight = '*';


    public function init_config(){

        $shopinfo = \app\common\model\xilumarket\Config::getMyConfig('shopinfo');
        $config['logo'] = isset($shopinfo['logo'])?cdnurl($shopinfo['logo'],true) :'';

        $data = \app\common\model\xilumarket\ChatEmoji::where('status', 'normal')->order('weigh desc,id desc')->select();
        $arr = [];
        foreach ($data as $key => $value) {
            $arr[] = [
                'name' => $value['name'],
                'image' => cdnurl($value['image'],true),
            ];
        }
        $this->success('',['config'=>$config,'emo'=>$arr]);
    }

    /**
     * 图片上传
     */
    public function params()
    {
        $name = $this->request->post('name');
        $md5 = $this->request->post('md5');
        $auth = new \addons\alioss\library\Auth();
        $params = $auth->params($name, $md5);
        config('default_return_type','json');//返回json而不是html
        $this->success('', $params);
    }

    /**小程序二维码图片上传
     * @param $filePath
     * @param $fileData
     * @return bool
     */
    public static function uploadAlioss($filePath, $fileData) {
        $config = get_addon_config('alioss');
        if(empty($config) || !isset($config['accessKeyId']))  return true;
        $accessKeyId = $config['accessKeyId'];
        $accessKeySecret = $config['accessKeySecret'];
        $endpoint = $config['endpoint'];
        $bucket = $config['bucket'];
        $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
        $ossClient->putObject($bucket, ltrim($filePath, '/'),$fileData);
        return true;
    }

    public function aftersale_config(){
        $config = [
            'express_type'  =>  [['id'=>0,'name'=>'未收到货'],['id'=>1,'name'=>"已收到货"]],
            'type'          =>  [['id'=>0,'name'=>'仅退款'],['id'=>1,'name'=>"退货退货"],['id'=>2,'name'=>'换货']],
            'reason'        => [
                ['id'=>0,'name'=>'不喜欢'],
                ['id'=>1,'name'=>"空包裹"],
                ['id'=>2,'name'=>'一直未送达'],
                ['id'=>3,'name'=>'颜色/尺码不符'],
                ['id'=>4,'name'=>'质量问题'],
                ['id'=>5,'name'=>'假冒品牌'],
            ]
        ];
        $this->success('',$config);
    }

    /**
     * 省市区树状图
     */
    public function area_tree(){
        $lists = Area::field("id,name")
            ->with(['childlist'=>function($query){
                $query->field(['id','name','pid'])
                    ->where('status','normal')
                    ->with(['childlist'=>function($query){
                        $query->where('status','normal')->field(['id','name','pid']);
                }]);
            }])
            ->where('level',1)
            ->where('status','normal')
            //->order('first','asc')
            ->select();
        $this->success('',$lists);
    }

    /**
     * 根据经纬度获取城市
     */
    public function get_city_by_lat(){
        $params = $this->request->param('');
        $city = Area::getCityFromLngLat($params['lng'],$params['lat']);
        if(!$city){
            $this->error('');
        }
        $this->success('',$city);
    }

    /**
     * 根据首字母获取城市
     */
    public function cities(){
        $list = Area::field("first")
            ->with(['children'=>function($query){
                $query->where('level',2)->where('status','normal')->field(['id','first','name','shortname']);
            }])
            ->where('status','normal')
            ->where('level',2)
            ->order('first','asc')
            ->group("first")
            ->select();
        $hot_cities = Area::where('level',2)->where('is_re',2)->where('status','normal')->field(['id','first','name','shortname'])->select();
        $this->success('',['hot_cities'=>$hot_cities,'cities'=>$list]);
    }

}